﻿Public Class masdrgitemwuse
    Private DTUID = Nothing
    Private DRGCODE = Nothing
    Private PRDCODE = Nothing
    Private USEID = Nothing
    Private TIMEID = Nothing
    Private STATUS = Nothing
    Private selectMasdrgitemwuse = "SELECT a.dtuid AS 'รหัสการจับคู่', b.genericname AS 'ชื่อสามัญ', b.tradename AS 'ชื่อทางการค้า', c.dusename AS 'การใช้ยา', d.dtname AS 'เวลาในการใช้ยา', a.status AS 'สถานะการเปิดใช้งาน', b.drgcode AS 'รหัสยา', b.prdcode AS 'รหัสผลิตภัณฑ์', c.useid AS 'รหัสการใช้ยา', d.timeid AS 'รหัสเวลาการใช้ยา'  FROM masdrgitemwuse a JOIN drugitem b ON a.drgcode = b.drgcode JOIN masdrguse c ON a.useid = c.useid JOIN masdrgtime d ON a.timeid = d.timeid"
    'Private selectMasdrgdoswuse = "SELECT USEID, DUCODE, DUSENAME, STATUS FROM masdrgdoswuse"
    'Private selectMasdrgacc = "SELECT DGTYID, DRGTYPE, convert(bit, [STATUS]) FROM MASDRGTYPE"

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property DTUID_ As Integer
        Get
            Return DTUID
        End Get
        Set(value As Integer)
            DTUID = value
        End Set
    End Property

    Property DRGCODE_ As String
        Get
            Return DRGCODE
        End Get
        Set(value As String)
            DRGCODE = value
        End Set
    End Property

    Property PRDCODE_ As Integer
        Get
            Return PRDCODE
        End Get
        Set(value As Integer)
            PRDCODE = value
        End Set
    End Property

    Property USEID_ As Integer
        Get
            Return USEID
        End Get
        Set(value As Integer)
            USEID = value
        End Set
    End Property

    Property TIMEID_ As Integer
        Get
            Return TIMEID
        End Get
        Set(value As Integer)
            TIMEID = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectMasdrgitemwuse_ As String
        Get
            Return selectMasdrgitemwuse
        End Get
        Set(value As String)

        End Set
    End Property

    'Public Function DrugData(ByVal comboboxName As ComboBox)
    '    Dim masdrgdosageData As DataTable
    '    masdrgdosageData = connect.GetTable("SELECT DRGCODE, DOSAGEFORM FROM masdrgdosage")
    '    Try
    '        With comboboxName
    '            .DataSource = masdrgdosageData
    '            .DisplayMember = "DOSAGEFORM"
    '            .ValueMember = "DFORM"
    '            '.BindingContext = BindingContext
    '        End With
    '    Catch ex As Exception
    '        MsgBox(ex.ToString)
    '    End Try
    'End Function

    Public Sub USEIDData(ByVal comboboxName As ComboBox)
        Dim masdrguseData As DataTable
        masdrguseData = connect.GetTable("SELECT useid, dusename FROM masdrguse")
        Try
            With comboboxName
                .DataSource = masdrguseData
                .DisplayMember = "DUSENAME"
                .ValueMember = "USEID"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub TIMEIDData(ByVal comboboxName As ComboBox)
        Dim masdrgtimeData As DataTable
        masdrgtimeData = connect.GetTable("SELECT timeid, dtname FROM masdrgtime")
        Try
            With comboboxName
                .DataSource = masdrgtimeData
                .DisplayMember = "DTNAME"
                .ValueMember = "TIMEID"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub insertMasdrgitemwuseData()
        If TypeOf DRGCODE Is String And TypeOf USEID Is Integer And TypeOf TIMEID Is Integer Then
            If connect.GetTable("SELECT dtuid FROM masdrgitemwuse WHERE drgcode = '" & DRGCODE & "' AND prdcode = '" & PRDCODE & "' and useid = '" & USEID & "' and timeid = '" & TIMEID & "' ").Rows.Count > 0 Then
                MsgBox("มีข้อมูลวิธีการใช้ยานี้อยู่ในระบบแล้ว")
            Else
                Try
                    connect = CONDB.NewConnection()
                    'connectlocal = conlocal.NewConnection()
                    'Dim dischargeID As Integer

                    connect.ExecuteNonQuery("INSERT INTO masdrgitemwuse (drgcode, prdcode, useid, timeid, status) VALUES ('" & DRGCODE & "', '" & PRDCODE & "', '" & USEID & "', '" & TIMEID & "', '" & STATUS & "')")
                    'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
                    'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
                    MsgBox("ท่านได้ทำการเพิ่มข้อมูลวิธีการใช้ยาเรียบร้อยแล้ว")
                Catch ex As Exception
                    MsgBox(vbCrLf & ex.Message)
                End Try
                connect.Dispose()
                'connectlocal.Dispose()
            End If
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง")
        End If
    End Sub

    Public Sub editMasdrgitemwuseData()
        If TypeOf DRGCODE Is String And TypeOf USEID Is Integer And TypeOf TIMEID Is Integer Then
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteNonQuery("UPDATE masdrgitemwuse SET drgcode = '" & DRGCODE & "', prdcode = '" & PRDCODE & "', useid = '" & USEID & "', timeid = '" & TIMEID & "', status = '" & STATUS & "' WHERE dtuid = '" & DTUID & "'")
                'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
                MsgBox("ท่านได้ทำการแก้ไขข้อมูลวิธีการใช้ยาเรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง")
        End If
    End Sub
End Class